import os
import requests
import json
import warnings

"""
南昌八一起义纪念馆，预约脚本

环境变量设置
  BY_Date = 预约的日期 例：2024-05-01@2024-05-02@2024-05-03@2024-05-04
  BY_Authorization = 小程序上的token，需要自己抓包（小程序：南昌八一起义纪念馆）
  BY_Period = 预约的时段 例：09:00-12:00@12:00-16:00
  BY_User = 预约的用户 例：张三-身份证-手机号@李四-身份证-手机号@王五-身份证-手机号
  PUSH_KEY = pushdeer 的key
"""

# 禁用不安全的请求警告
warnings.filterwarnings("ignore", message="Unverified HTTPS request")


def order(name, mobile, cardId, arriveTime, startTime, endTime, authorization):

    url = "https://byguan.jxicp.com/account/order"

    payload = json.dumps(
        {
            "siteId": 1,
            "paymentId": 10002,
            "name": name,
            "mobile": mobile,
            "cardId": cardId,
            "cardType": 0,
            "dept": None,
            "orderType": 1,
            "arriveTime": arriveTime,
            "scan": False,
            "startTime": startTime,
            "endTime": endTime,
            "orderSource": "",
            "orderGoods": [
                {"productId": 1, "quantity": 1},
                {"productId": 3, "quantity": 0},
            ],
        }
    )

    headers = {
        "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/116.0.0.0 Safari/537.36 MicroMessenger/7.0.20.1781(0x6700143B) NetType/WIFI MiniProgramEnv/Windows WindowsWechat/WMPF WindowsWechat(0x63090a13) XWEB/9115",
        "Content-Type": "application/json",
        "xweb_xhr": "1",
        "authorization": f"Bearer {authorization}",
        "sec-fetch-site": "cross-site",
        "sec-fetch-mode": "cors",
        "sec-fetch-dest": "empty",
        "referer": "https://servicewechat.com/wx12d2ab9f3b262888/16/page-frame.html",
        "accept-language": "zh-CN,zh;q=0.9",
    }

    response = requests.post(url, data=payload, headers=headers, verify=False)

    return response.json()


def send(message, PUSH_KEY):
    url = f"https://api2.pushdeer.com/message/push?pushkey={PUSH_KEY}&text={message}"

    response = requests.get(url, verify=False)

    if response.status_code == 200:
        print(response.text)


def main():
    PUSH_KEY = os.environ.get("PUSH_KEY")
    BY_Date = os.environ.get("BY_Date")
    BY_Authorization = os.environ.get("BY_Authorization")
    BY_Period = os.environ.get("BY_Period")
    BY_User = os.environ.get("BY_User")

    dates = BY_Date.split("@")
    periods = BY_Period.split("@")
    users = BY_User.split("@")

    for date in dates:
        print("-------------", date, "-------------")
        for period in periods:
            print(f"\t{period}")
            startTime = period.split("-")[0]
            endTime = period.split("-")[1]
            for user in users:
                name = user.split("-")[0]
                cardId = user.split("-")[1]
                mobile = user.split("-")[2]
                order_data = order(
                    name, mobile, cardId, date, startTime, endTime, BY_Authorization
                )

                if order_data.get("orderNo") is not None:
                    send(f"{date} {period} {name} 预约成功", PUSH_KEY)

                print(f"\t\t{name} 预约返回 {order_data}")


if __name__ == "__main__":
    main()
